package com.stars.empoly.activity

import android.os.Bundle
import androidx.lifecycle.lifecycleScope
import com.qmuiteam.qmui.kotlin.onClick
import com.stars.empoly.adapter.TechListAdapter
import com.stars.empoly.base.MBRActivity
import com.stars.empoly.databinding.ActivityTechListBinding
import com.stars.empoly.inte.ChoiceData
import com.stars.empoly.inte.ChoiceLocation
import com.stars.empoly.utils.CurrentOneLocationUtils
import com.stars.empoly.utils.PopuBuyUtils
import com.stars.empoly.utils.PopuListUtils
import com.stars.empoly.vmodel.TechListVModel
import com.tencent.mmkv.MMKV
import java.util.ArrayList

/**
 * 技师列表
 *on 2022/9/30
 */
class TechListActivity: MBRActivity<TechListVModel, TechListAdapter, ActivityTechListBinding>() {

    override fun init(savedInstanceState: Bundle?) {
        super.init(savedInstanceState)

        vm.technicianServiceId=intent.extras?.getString("id")
        //获取当前城市
        lifecycleScope.launchWhenCreated {
            CurrentOneLocationUtils.getStartLocal(context() as TechListActivity, object :
                ChoiceLocation {
                override fun onRestuse(lat: String, lng: String, city: String) {
                    binding.tvCityName.text=city
                    MMKV.defaultMMKV().putString("city", city)
                    MMKV.defaultMMKV().putString("lat", lat)
                    MMKV.defaultMMKV().putString("lng", lng)
                }
            })
        }
        //获取城市的Id
        vm.getCityList()
        var cityName=MMKV.defaultMMKV().getString("city", "")
        vm.cityList.observe(this){
            if(it.isEmpty()){
                return@observe
            }
            for(item in it){
                if(item.name == "cityName"){
                    vm.cityId=item.id
                    //刷新
                    onRefresh()
                }
            }
        }



        vm.bean.observe(this){
            //去下单

            PopuBuyUtils.show(context(),it,binding.ll)

        }
    }


    override fun listeners() {
        super.listeners()
        binding.llCity.onClick {
            if(null!=vm.cityList.value&& vm.cityList.value!!.isNotEmpty()){

                var list= ArrayList<String>()

                for(item in vm.cityList.value!!){
                    list.add(item.name)
                }
                PopuListUtils.showQMUIPopup(context(),binding.llCity,list,object : ChoiceData {
                    override fun onRestuse(context: String?) {
                        for(item in vm.cityList.value!!){
                            if(item.name == context){
                                vm.cityId=item.id
                                binding.tvCityName.text=item.name
                                onRefresh()
                            }
                        }

                    }

                })


            }
        }
    }
    override fun vmClass(): Class<TechListVModel> {
        return TechListVModel::class.java
    }
    override fun binding(): ActivityTechListBinding {
        return ActivityTechListBinding.inflate(layoutInflater)
    }

    override fun initAdapter(): TechListAdapter {
       return TechListAdapter(vm)
    }

    override fun title(): String? {
        return "技师"
    }
}
